package timing.ukulele.storage.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import timing.ukulele.storage.dictionary.RoleTypeEnum;
import timing.ukulele.storage.handler.LoginFailureHandler;
import timing.ukulele.storage.handler.LoginSuccessHandler;
import timing.ukulele.storage.handler.MyLogoutSuccessHandler;
import timing.ukulele.storage.jwt.JWTFilter;
import timing.ukulele.storage.jwt.JWTProvider;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JWTProvider jwtProvider;
    @Autowired
    private LoginSuccessHandler loginSuccessHandler;
    @Autowired
    private LoginFailureHandler loginFailureHandler;
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)// 设置自定义的userDetailsService
                .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)//设置无状态
                .and()
                .authorizeRequests() // 配置请求权限
                .antMatchers("/group/**", "/user/**").hasAuthority(RoleTypeEnum.ADMIN.name()) // 需要角色
                .anyRequest().authenticated() // 所有的请求都需要登录
                .and()
                // 配置登录url，和登录成功处理器
                .formLogin().loginProcessingUrl("/login")
                .successHandler(loginSuccessHandler).failureHandler(loginFailureHandler)
                // 取消csrf防护
                .and().csrf().disable()
                .httpBasic()
                // 配置登出url，和登出成功处理器
                .and().logout().logoutUrl("/logout")
                .logoutSuccessHandler(myLogoutSuccessHandler)
                // 在UsernamePasswordAuthenticationFilter之前执行我们添加的JWTFilter
                .and().addFilterBefore(new JWTFilter(jwtProvider), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}